$(document).ready(function() {
	getParam();
	SetClosYM();
	$("#inputMonth").attr('disabled','disabled');
	$("#inputYear").attr('disabled','disabled');
	
});

function Search(){

	var peNo = $("#inputPeNo").val();
	var month =$("#inputMonth").val();
	var year =$("#inputYear").val();
	
	if (month == "" || year == "") {
		document.getElementById("modal_content").innerHTML = "กรุณาเลือกดือนและปีที่จะค้นหา";
		$('#warningModal').modal();
	}else{

		$.ajax({
			url : 'ACM1102Srvl', 
			type : "POST",
			data : {
				peNo: peNo,
				month:month,
				year: year
			},
			dataType : "json",
			success: function(data){
				var message = "";
				message +="<table class='table-list table-bordered table-hover'>";
				message +=" <thead>";
				message +=" <tr>";
				message +=" <th>ลำดับ</th>"; 
				message +=" <th>รหัสตัวแทน</th>";  
				message +=" <th>ชื่อสกุล</th>";  
				message +=" <th>NET MOV</th>";  
				message +=" <th>จัดการ</th>";  
				message +=" </tr>";
				message +=" </thead>";   
				message +=" <tbody>";  
				message +=" <tr>";
				for (var i = 0; i < data.PeNo.length; i++) {
					var seq= i+1;
				message +=" <td>"+seq+"</td>";
				message +=" <td >"+data.PeNo[i]+"</td>";
				message +=" <td style='text-align:left;' >"+data.name[i]+"</td>";
				message +=" <td style='text-align:right;'>"+data.net[i]+"</td>";
				message +=" <td><button class='btn btn-primary mr-5' onclick='next("+data.PeNo[i]+","+year+","+0+month+");'><i class='fa fa-external-link' title='ดูรายละเอียด' id='editButton' ></i></button><button class='btn btn-primary' onclick='details("+data.PeNo[i]+","+year+","+0+month+");' ><i class='fa fa-search' title='รายละเอียดPopUp' id='editButton' ></i></button></td>";
				message +=" </tr>";
				}
				message +=" </tbody>";   
				message +=" </table>";   
				$("#table-search").html(message);
			}
			
		});
	}
	
}

function next(PeNo,year,month){
	//alert(month);
	//alert($("#inputMonth").val()+"     "+$("#inputYear").val());
	$("#PeNo").val(PeNo);
	$("#Month").val($("#inputMonth").val());
	$("#Year").val($("#inputYear").val());
	//alert(PeNo +"  "+month+"  "+year);
	document.getElementById("details").submit();
			
			

	// window.open("ACM11022.jsp", "_blank");
	//window.open("ACM11022.jsp", "_blank");
	
}

function details(PeNo,year,month){
	//alert("details"+PeNo+"  "+0+month+"  "+year);
	var months = "0"+month;
	//alert(months);
	$.ajax({
		url : 'ACM1102Srvl', 
		type : "GET",
		data : {
			peNo: PeNo,
			month:months,
			year: year
		},
		dataType : "json",
		success: function(data){
			var message = "";
			message +="<table class='table-list table-bordered table-hover'>";
			message +=" <thead>";
			message +=" <tr>";
			message +=" <th>ลำดับ</th>";
			message +=" <th>รหัสตัวแทน</th>";  
			message +=" <th>ชื่อสกุล</th>";
			message +=" <th>Duty Code</th>";
			message +=" <th>PC</th>";
			message +=" </tr>";
			message +=" </thead>";   
			message +=" <tbody>";  
			
			for (var i = 0; i < parseInt(data.PeNo.length) ; i++) {
				var seq= i+1;
			message +=" <tr>";
			message +=" <td>"+seq+"</td>";
			message +=" <td>"+data.PeNo[i]+"</td>";
			message +=" <td style='text-align:left;'>"+data.name[i]+"</td>";
			message +=" <td>"+data.duty[i]+"</td>";
			message +=" <td style='text-align:right;'>"+data.pc[i]+"</td>";
			message +=" </tr>";
			}
			message +=" <tr>";
			message +=" <td colspan='4'>Total PC</td>";
			message +=" <td ><b>"+data.total+"</b></td>";
			message +=" </tr>";
			message +=" </tbody>";   
			message +=" </table>";   
			
			document.getElementById("table-details").innerHTML= message;
			$("#detailsModal").modal('show');
		}
		
	});
	
	
	
}

function getParam(name){
	   if(name=(new RegExp('[?&]'+encodeURIComponent(name)+'=([^&]*)')).exec(location.search))
	      return decodeURIComponent(name[1]);
}
function SetClosYM(){
	//alert("closym = "+getParam('clos_ym'));
	var closym = getParam('clos_ym');
	var year = closym.substring(0, 4);
	var month = closym.substring(4, 6);
	
	$("#inputMonth option[value='"+month+"']").attr("selected", "selected");
	$("#inputYear option[value='"+year+"']").attr("selected", "selected");
	Search();
}









